package com.fuluwa.interfaces.knowledge.web;

import org.apache.commons.fileupload.ProgressListener;

public class FileUploadListener implements ProgressListener {

	protected long num100Ks = 0;
	protected long bytesRead = 0; // 已读字节数
	protected long contentLength = -1; // 总字节数
	protected int itemIndex = 0; // 当前第几个文件
	protected int percentage = 0; // 完成百分比
	protected boolean contentLengthKnown = false;

	@Override
	public void update(long pBytesRead, long pContentLength, int pItems) {

		if (pContentLength > -1) {
			contentLengthKnown = true;
		}
		bytesRead = pBytesRead;
		contentLength = pContentLength;
		itemIndex = pItems;

		long nowNum100Ks = pBytesRead / 100000;
		if (nowNum100Ks > num100Ks) {
			num100Ks = nowNum100Ks;
			if (contentLengthKnown) {
				percentage = (int) Math.round(100.00 * pBytesRead / pContentLength);
			}

		}
	}

	public int getPercentage() {
		return percentage;
	}
}